/*
 * @Author: your name
 * @Date: 2020-08-25 11:56:47
 * @LastEditTime: 2020-08-28 21:16:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \undefinedd:\1712实训\实训一\资料\1711A实训一\三组\代码\patient\src\utils\fetch.js
 */
import axios from 'axios'
// import cookie from '@/utils/cookie'
import vm from '@/main'

import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

const server = axios.create({
  // timeout: 4000,
  baseURL: 'http://47.101.45.245:8878'
})

const whiteList = ['/v1/token', '/v1/registry']

server.interceptors.request.use((config) => {
  // 在发送请求之前做些什么
  if (!whiteList.includes(config.url)) {
    //判断是 移动端还是pc端
    config.headers.authorization = vm.$route.path.indexOf('/home')>-1 || vm.$route.path.indexOf('/doclogin')>-1 || vm.$route.path.indexOf('/consult')>-1 || vm.$route.path.indexOf('/recharge') > -1 ? 'JWT ' + localStorage.getItem('patient_token') : 'JWT ' + localStorage.getItem('doctor_token')
    console.log(config.headers.authorization)
  }
  // config.headers.authorization =
  Nprogress.start() //
  return config
}, (error) => {
  // 对请求错误做些什么
  return Promise.reject(error)
})

// 添加响应拦截器
server.interceptors.response.use((response) => {
  // 对响应数据做点什么
  Nprogress.done()
  return response.data
}, (error) => {
  console.log(error.response)
  const response = error.response
  if (response.status === 401) {
    // cookie.remove('token')
    vm.$message.error(response.data.message)
    console.log(vm.$router)
    vm.$router.push({
      path: 'login',
      query: {
        callbcakUrl: vm.$route.path
      }
    })
  } else if (response.status === 422) {
    vm.$message.error(response.data.message)
  }
  // 对响应错误做点什么
  return Promise.reject(error)
})

export default {
  ...server
  // get: (url, data) => server.get(url, {
  //   params: data
  // }),
  // post: (url, data) => server.post(url, {...data})
}
